﻿using Newtonsoft.ViewModelBase;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using ZoneLineNew.Core.Helpers;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;

namespace ZoneLineNew.ViewModelBase
{
    /// <summary>
    /// 弹窗ViewModel
    /// </summary>
    public class DialogViewModel : BaseViewModel, IDialogAware, IDataErrorInfo
    {
        public Window Owner { get; set; } = Application.Current?.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);

        protected readonly IRegionManager _regionManager;
        protected readonly IDialogService _dialog;
        protected readonly IEventAggregator _eventAggregator;
        protected readonly IMessageService _message;
        protected readonly IDbOperate<IFreeSqlMainContext> _mainDbOperate;

        protected IDialogParameters _dialogParameters = null;//弹框参数

        public DialogViewModel(
            IRegionManager regionManager, 
            IDialogService dialog, 
            IEventAggregator eventAggregator, 
            IMessageService message, IDbOperate<IFreeSqlMainContext> mainDbOperate) {
            _regionManager = regionManager;
            _dialog = dialog;
            _eventAggregator = eventAggregator;
            _message = message;
            _mainDbOperate = mainDbOperate;
        }

        public string Error => string.Empty;

        #region 弹窗基本属性

        private string _Title;

        public string Title
        {
            get { return _Title; }
            set { _Title = value; RaisePropertyChanged(); }
        }
        

        private double _Width = 360;

        public double Width
        {
            get { return _Width; }
            set { _Width = value; RaisePropertyChanged(); }
        }
        

        private double _Height = 180;

        public double Height
        {
            get { return _Height; }
            set { _Height = value; RaisePropertyChanged(); }
        }

        public event Action<IDialogResult> RequestClose;

        #endregion

        #region 弹窗事件
        public virtual bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
        }

        public virtual void OnDialogClosed(ButtonResult button)
        {
            RequestClose?.Invoke(new DialogResult(button));
        }

        public virtual void OnDialogClosed(IDialogResult dialogResult)
        {
            RequestClose?.Invoke(dialogResult);
        }

        public virtual void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters.ContainsKey("Title"))
            {
                Title = parameters.GetValue<string>("Title");
            }
            if (parameters.ContainsKey("Width"))
            {
                Width = parameters.GetValue<double>("Width");
            }
            _dialogParameters = parameters;
        }
        #endregion

        #region 数据验证
        public string this[string columnName]
        {
            get
            {
                return ValidationHelper.ValidateProperty(this, columnName);
            }
        }
        #endregion

    }
}
